package com.credit.jumu.calculator.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.credit.jumu.calculator.R;
import com.credit.jumu.calculator.activity.HouseResultActivity_;
import com.credit.jumu.calculator.base.BaseFragment;
import com.credit.jumu.calculator.utils.AddressUtil;
import com.credit.jumu.calculator.utils.DensityUtil;
import com.credit.jumu.calculator.utils.JsonUtils;
import com.credit.jumu.calculator.utils.LogUtils;
import com.credit.jumu.calculator.utils.MoneyTextWatcher;
import com.credit.jumu.calculator.utils.StringHandleUtil;
import com.credit.jumu.calculator.utils.ToastUtil;
import com.kyleduo.switchbutton.SwitchButton;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

import cn.qqtheme.framework.picker.OptionPicker;
import cn.qqtheme.framework.widget.WheelView;

/**
 * Created by Administrator on 2017/11/23/023.
 */
@EFragment(R.layout.fragment_house)
public class HouseFragment extends BaseFragment {
	@ViewById
	EditText area, presentprice, originalprice;//面积,现价,原价
	@ViewById
	SwitchButton idfirstchoose, idonlychoose;//是否首次购房,是否唯一
	@ViewById(R.id.houseType)
	TextView houseTypeCHoose;
	@ViewById(R.id.buylong)
	TextView buylong;
	@ViewById
	View longLine;

	int houseType = 0;
	int houseLong = 0;
	boolean isFirstBuy = true;
	boolean isOnlyBuy = false;
	@ViewById
	RelativeLayout allTime;


	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}


	@Override
	public void onResume() {
		super.onResume();

	}


	@AfterViews
	public void initView() {
		area.addTextChangedListener(new MoneyTextWatcher(area));
		presentprice.addTextChangedListener(new MoneyTextWatcher(presentprice));
		originalprice.addTextChangedListener(new MoneyTextWatcher(originalprice));

		idfirstchoose.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				isFirstBuy = isChecked;
			}
		});
		idonlychoose.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				isOnlyBuy = isChecked;
			}
		});
	}

	@Click(R.id.togetresult)
	public void getResult() {
		double houseArea = StringHandleUtil.toInt(area.getText().toString());//面积
		double nowprice = StringHandleUtil.toInt(presentprice.getText().toString());//现价
		double oldprice = StringHandleUtil.toInt(originalprice.getText().toString());//原价
		if (0 >= houseArea || 0 >= nowprice || 0 >= oldprice) {
			ToastUtil.showShortToast("信息不完善或格式不正确!");
			return;
		}
		LogUtils.println(houseArea + "------zhangyiyi-----" + nowprice);
		Intent intent = new Intent(getActivity(), HouseResultActivity_.class);
		intent.putExtra("houseArea", houseArea);//面积
		intent.putExtra("nowprice", nowprice);//现价
		intent.putExtra("oldprice", oldprice);//原价
		intent.putExtra("houseType", houseType);//类型
		intent.putExtra("houseLong", houseLong);//年限
		intent.putExtra("isFirstBuy", isFirstBuy);//是否首次
		intent.putExtra("isOnlyBuy", isOnlyBuy);//是否唯一
		startActivity(intent);
	}

	@Click(R.id.houseTypeLayout)
	public void houseTypeClick() {

		OptionPicker picker = new OptionPicker(getActivity(), new String[]{
				"普通住宅", "非普通住宅", "经济适用房"
		});
		picker.setCycleDisable(true);//设置启用循环
//		picker.setItemWidth(200);
		picker.setLineSpaceMultiplier(1);//总高度(1-4)
		picker.setCanceledOnTouchOutside(false);
		picker.setDividerRatio(WheelView.DividerConfig.FILL);
		picker.setShadowColor(Color.BLACK, 1);//选中的背景色
		picker.setSelectedIndex(0);
		picker.setTopLineColor(getResources().getColor(R.color.transparent));//分割线颜色
		picker.setSubmitTextColor(getResources().getColor(R.color.titlettextbg));//右边文字
		picker.setCancelTextColor(getResources().getColor(R.color.titlettextbg));//左边文字
		picker.setTextSize(DensityUtil.dip2px(getContext(), 7));//文字大小
		picker.setTextColor(getResources().getColor(R.color.back));
		picker.setDividerColor(getResources().getColor(R.color.transparent));
		picker.setTopBackgroundColor(getResources().getColor(R.color.linebg));//顶部背景
		picker.setCanceledOnTouchOutside(true);
		picker.setSubmitText("完成");
		picker.setOnOptionPickListener(new OptionPicker.OnOptionPickListener() {
			@Override
			public void onOptionPicked(int index, String item) {
				houseTypeCHoose.setText(item);
				houseType = index;
				if (index == 2) {
					allTime.setVisibility(View.GONE);
					longLine.setVisibility(View.GONE);
				} else {
					allTime.setVisibility(View.VISIBLE);
					longLine.setVisibility(View.VISIBLE);
				}
			}
		});
		picker.show();
	}

	@Click(R.id.allTime)
	public void buylongClick() {
		OptionPicker picker = new OptionPicker(getActivity(), new String[]{
				"不满2年", "满2年不满5年", "满5年"
		});
		picker.setCycleDisable(true);//设置启用循环
		picker.setLineSpaceMultiplier(1);//总高度(1-4)
		picker.setCanceledOnTouchOutside(false);
		picker.setDividerRatio(WheelView.DividerConfig.FILL);
		picker.setShadowColor(Color.BLACK, 1);//选中的背景色
		picker.setSelectedIndex(0);
		picker.setTopLineColor(getResources().getColor(R.color.transparent));//分割线颜色
		picker.setSubmitTextColor(getResources().getColor(R.color.titlettextbg));//右边文字
		picker.setCancelTextColor(getResources().getColor(R.color.titlettextbg));//左边文字
		picker.setTextSize(DensityUtil.dip2px(getContext(), 7));//文字大小
		picker.setTextColor(getResources().getColor(R.color.back));
		picker.setSubmitText("完成");
		picker.setDividerColor(getResources().getColor(R.color.transparent));
		picker.setTopBackgroundColor(getResources().getColor(R.color.linebg));//顶部背景
		picker.setCanceledOnTouchOutside(true);
		picker.setOnOptionPickListener(new OptionPicker.OnOptionPickListener() {
			@Override
			public void onOptionPicked(int index, String item) {

				houseLong = index;
				buylong.setText(item);
			}
		});
		picker.show();
	}

}
